package com.ruoyi.capsule.task;

import com.ruoyi.capsule.domain.CapsuleData;
import com.ruoyi.capsule.service.ICapsuleDataService;
import com.ruoyi.capsule.utils.SpringContextUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class CapsuleTask {

    public void runTask(String capsuleId) throws SchedulerException {
        // 直接使用注入的Service
        System.out.println("执行任务：" + capsuleId+new Date());
        CapsuleData capsuleData = new CapsuleData();
        capsuleData=SpringUtils.getBean(ICapsuleDataService.class).selectCapsuleDataByCapsuleId(Long.parseLong(capsuleId));


        //发送给mq中间件
        SpringUtils.getBean(RabbitTemplate.class).convertAndSend("capsule_exchange","capsule_routingKey",capsuleData);
//        SpringContextUtil.getBean(RabbitTemplate.class).convertAndSend("capsule_exchange","capsule_routingKey",capsuleData);
        //暂停该定时任务
        ISysJobService iSysJobService = SpringUtils.getBean(ISysJobService.class);
        SysJob sysJob = new SysJob();
        SysJob newJob = iSysJobService.selectJobById(Long.valueOf(capsuleId));
        newJob.setStatus("1");
        iSysJobService.changeStatus(newJob);
        // 其他业务逻辑...
    }
}
